//
//  WCNInvitePatientVC.m
//  WCN
//
//  Created by wsj2015 on 16/1/30.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNInvitePatientVC.h"
#import "MyAttributedStringBuilder.h"
#import "WCNFansModel.h"
#import "UITableView+(removeMjrefreshFooter).h"
#import "WCNFansCell.h"
#import "UINavigationItem+Offset.h"
@interface WCNInvitePatientVC ()<UIAlertViewDelegate>


@property (nonatomic,  retain)  NSMutableArray *dataArray;
@property (nonatomic,  assign) NSInteger totalNum;
@property (nonatomic,  retain) UIButton *footerBtn;


@property (nonatomic, assign) NSInteger currentfans;
@property(nonatomic, assign) NSInteger pageForFans;
@end

@implementation WCNInvitePatientVC
//{
//    NSInteger _pageForFans;
//}

- (instancetype)init{
    if (self = [super init]) {
        self.title = @"邀请上传";
        _pageForFans =1;
        self.currentfans = -1;
    }
    return self;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self configUI];
    self.totalNum = 0;
    [self getFansWithIsReCover:YES];
    
}
- (void)configUI{
    
//    UIView *headView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 75)];
//    headView.tag = 100;
//    headView.backgroundColor = kAppBg;
//    [self.view addSubview:headView];
//    UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(20, 0, kScreenWidth -40, 75)];
//    label.tag = 200;
//    label.numberOfLines =3;
//    label.font = [UIFont systemFontOfSize:15];
//    [headView addSubview:label];
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight  -50 -64);
    [self.tableView registerNib:[UINib nibWithNibName:@"WCNFansCell" bundle:nil] forCellReuseIdentifier:kFansReuseId];
    [self.view addSubview:self.footerBtn];
    weakSelf_define;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        _pageForFans =1;
        [weakSelf getFansWithIsReCover:YES];
    }];
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        weakSelf.pageForFans ++;
        [weakSelf getFansWithIsReCover:NO];
    }];
}


- (UIButton *)footerBtn{
    if (!_footerBtn) {
        _footerBtn =[UIButton buttonWithType:UIButtonTypeSystem];
        _footerBtn.frame =CGRectMake(30, kScreenHeight -35-64-7, kScreenWidth -60, 35);
        [_footerBtn setTitle:@"邀请上传档案" forState:UIControlStateNormal];
        [_footerBtn setBackgroundColor:HexRGB_WSJ(84, 130, 255)];
        _footerBtn.layer.cornerRadius = 5;
        _footerBtn.layer.masksToBounds= YES;
        [_footerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_footerBtn addTarget:self action:@selector(comfirmToInvite) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _footerBtn;
    
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[NSMutableArray array];
    }
    return _dataArray;
}
#pragma mark -action
- (void)comfirmToInvite{
    
    if (self.currentfans ==-1) {
        [[WCPopupLayerManager shareMg] showTextOnly:@"请选择一个患者" withView:self.view];
        return;
    }
    UIAlertView *alerView =[[UIAlertView alloc] initWithTitle:@"提示" message:@"是否确认邀请信息" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
    [alerView show];
    
    

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex ==1) {
        [self sendInvitationToApatient];
    }
}

#pragma mark -发送邀请
- (void)sendInvitationToApatient{
//    sourceId 源ID (必填)
//    messageType 消息类型 (3 普通文本消息 必填)
//    messageTitle 消息标题 (必填)
//    messageContent 消息内容 (必填)
//    targetId 接收人ID(必填)
//    targetType 接收人类型 (必填 1、护士 2、患者)
    CoreSVPLoading(@"发送邀请信息", NO);
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    
    WCNFansModel *fans = self.dataArray[self.currentfans];
    params[@"nurseId"] =@([[WCDefaultManager shareMg] nurseId]);
    params[@"patientId"] =@([fans.wcId integerValue]);
   

    [[WCNetworkAccessManager sharedMg] POST:kInvitePatientUpdate parameters:params success:^(NSDictionary *responseObject) {
        
        CoreSVPSuccess(responseObject[@"data"]);
    } failure:nil netError:nil always:^{
        [CoreSVP dismiss];
    }];
    
    
}


#pragma mark -请求关注我的患者列表
- (void)getFansWithIsReCover:(BOOL)isCover
{
    MBProgressHUD *hud =showHud;
    weakSelf_define;
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"nurseAccountId"] =@([[WCDefaultManager shareMg] nurseId]);
    //    params[@"category"] =@1;
    params[@"page"] =@(_pageForFans);
    params[@"rows"] =@8;
    [[WCNetworkAccessManager sharedMg] POST:kFans_part parameters:params success:^(NSDictionary  *responseObject) {
        NSDictionary *dataDict =responseObject[@"data"];
        if (isCover) {
            [weakSelf.dataArray removeAllObjects];
             weakSelf.currentfans = -1;
//            weakSelf.totalNum =[dataDict[@"total"] integerValue];
        }
        
        
        for (NSDictionary *fansDict in dataDict[@"result"]) {
            WCNFansModel *fans =[[WCNFansModel alloc]init];
            [fans setValuesForKeysWithDictionary:fansDict];
            [weakSelf.dataArray addObject:fans];
        }
        [UITableView remvoMJRefreshFooter:_pageForFans totalPage:[dataDict[@"totalPage"] integerValue] forTableView:weakSelf.tableView withFootFreshBlock:^{
            _pageForFans ++;
            [weakSelf getFansWithIsReCover:NO];
            
        }];
        [weakSelf.tableView reloadData];
        
    } failure:^(NSString *msg) {
        
    } netError:nil always:^{
        [hud hide:YES];
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
       
    }];
    
    
    
    
}


- (void)setTotalNum:(NSInteger)totalNum{
    _totalNum = totalNum;
    NSString *str =[NSString stringWithFormat:@"您还有以下%i位关注用户未上传档案,您可以发送消息邀请用户上传档案",totalNum];
    UIView *headView =[self.view viewWithTag:100];
    UILabel *label =[headView viewWithTag:200];
//    label.attributedText =
    MyAttributedStringBuilder *builder =[[MyAttributedStringBuilder alloc] initWithString:str];
    
    MyAttributedStringRange *attrRang =[builder characterSet:[NSCharacterSet  decimalDigitCharacterSet]];
    
    attrRang.textColor = [UIColor orangeColor];
    attrRang.font =[UIFont systemFontOfSize:20];
    
    label.attributedText  =builder.commit;

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WCNFansCell *cell =[tableView dequeueReusableCellWithIdentifier:kFansReuseId forIndexPath:indexPath];
    WCNFansModel *model =self.dataArray[indexPath.row];
  
    if (indexPath.row ==self.currentfans) {
        model.isSelect = YES;
    }else{
        model.isSelect = NO;
    }
    [cell configFansModel:model];
   
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 64;
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
   return @"请选择患者";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    WCNFansModel *model =self.dataArray[indexPath.row];
    
    self.currentfans = indexPath.row;
    [self.tableView reloadData];
//    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationFade];
    
}

@end
